package com.itheima.springmvc.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.itheima.springmvc.pojo.Items;
import com.itheima.springmvc.pojo.QueryVo;
import com.itheima.springmvc.service.ItemService;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;

	@RequestMapping(value = "/item/itemlist.action")
	public ModelAndView itemList() throws Exception {
		// if(1==1) {
		// throw new MyException("hello");
		// }
		// 创建页面显示的商品数据
		List<Items> list = itemService.selectItemsList();

		ModelAndView mav = new ModelAndView();
		mav.addObject("itemList", list);
		mav.setViewName("itemList");
		return mav;
	}

	// 去修改页面的入参id
	@RequestMapping(value = "/itemEdit.action")
	public ModelAndView toEdit(Integer id, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model model) {

		// servle开发
		// String id = request.getParameter("id");

		// 查询一个商品
		// Items items = itemService.selectItemsById(Integer.parseInt(id));
		Items items = itemService.selectItemsById(id);
		ModelAndView mav = new ModelAndView();
		mav.addObject("item", items);
		mav.setViewName("editItem");
		return mav;
	}

	@RequestMapping(value = "/updateitem.action")
	// public ModelAndView updateItems(Items items) {
	public String updateItems(QueryVo vo, MultipartFile pictureFile) throws Exception {
		// 设置图片名字，不能重复，可以使用UUID
		String picName = UUID.randomUUID().toString().replaceAll("-", "");
		// 获得文件名
		String oriName = pictureFile.getOriginalFilename();
		// 获得图片后缀
		String extName = oriName.substring(oriName.lastIndexOf("."));

		// 开始上传
		pictureFile.transferTo(new File("d:/upload/" + picName + extName));

		Items items = vo.getItems();
		items.setPic(picName + "." + extName);
		itemService.updateItemsById(items);
		// ModelAndView mav = new ModelAndView();
		// mav.setViewName("success");
		// 重定向
		return "redirect:/item/itemlist.action";
	}

	// 删除多个
	@RequestMapping(value = "/deletes.action")
	public ModelAndView deleteItems(QueryVo vo) {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("success");
		return mav;
	}

	// 修改多个
	@RequestMapping(value = "/updates.action")
	public ModelAndView updates(QueryVo vo) {
		for (Items items : vo.getItemsList()) {
			itemService.updateItemsById(items);
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("itemList", vo.getItemsList());
		mav.setViewName("itemList");
		return mav;
	}

	// json数据交互
	@RequestMapping(value = "/json.action")
	public @ResponseBody Items updates(@RequestBody Items items) {
		return items;
	}

	// restful风格开发
	@RequestMapping(value = "/itemEdit/{id}.action")
	public ModelAndView toEdit1(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model model) {

		// servle开发
		// String id = request.getParameter("id");

		// 查询一个商品
		// Items items = itemService.selectItemsById(Integer.parseInt(id));
		Items items = itemService.selectItemsById(id);
		ModelAndView mav = new ModelAndView();
		mav.addObject("item", items);
		mav.setViewName("editItem");
		return mav;
	}

	//登录
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String login() {
		return "login";
	}
	
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String username, HttpSession httpSession) {
		httpSession.setAttribute("USER_SESSION", username);
		return "redirect:/item/itemlist.action";
	}
}
